﻿/*
**  2D矩阵
*/

if (!$.Engine.Math || typeof $.Engine.Math === "undefined") $.Engine.Math = {};
if (!$.Engine.Math.Mat2D || typeof $.Engine.Math.Mat2D === "undefined") {
  $.Engine.Math.Mat2D = function (angle, c1, c2) {
    if (angle == null) angle = 0;
    // initialize instance variables for references
    this.col1 = new $.Engine.Math.Vector2D();
    this.col2 = new $.Engine.Math.Vector2D();
    //

    if (c1 != null && c2 != null) {
      this.col1.SetV(c1);
      this.col2.SetV(c2);
    }
    else {
      var c = Math.cos(angle);
      var s = Math.sin(angle);
      this.col1.x = c; this.col2.x = -s;
      this.col1.y = s; this.col2.y = c;
    }
  };
}
Object.extend($.Engine.Math.Mat2D.prototype, {
  Set: function (angle) {
    var c = Math.cos(angle);
    var s = Math.sin(angle);
    this.col1.x = c; this.col2.x = -s;
    this.col1.y = s; this.col2.y = c;
  },

  SetVV: function (c1, c2) {
    this.col1.SetV(c1);
    this.col2.SetV(c2);
  },

  Copy: function () {
    return new $.Engine.Math.Mat2D(0, this.col1, this.col2);
  },

  SetM: function (m) {
    this.col1.SetV(m.col1);
    this.col2.SetV(m.col2);
  },

  AddM: function (m) {
    this.col1.x += m.col1.x;
    this.col1.y += m.col1.y;
    this.col2.x += m.col2.x;
    this.col2.y += m.col2.y;
  },

  SetIdentity: function () {
    this.col1.x = 1.0; this.col2.x = 0.0;
    this.col1.y = 0.0; this.col2.y = 1.0;
  },

  SetZero: function () {
    this.col1.x = 0.0; this.col2.x = 0.0;
    this.col1.y = 0.0; this.col2.y = 0.0;
  },

  Invert: function (out) {
    var a = this.col1.x;
    var b = this.col2.x;
    var c = this.col1.y;
    var d = this.col2.y;
    var det = a * d - b * c;
    det = 1.0 / det;
    out.col1.x = det * d; out.col2.x = -det * b;
    out.col1.y = -det * c; out.col2.y = det * a;
    return out;
  },

  Solve: function (out, bX, bY) {
    var a11 = this.col1.x;
    var a12 = this.col2.x;
    var a21 = this.col1.y;
    var a22 = this.col2.y;
    var det = a11 * a22 - a12 * a21;
    det = 1.0 / det;
    out.x = det * (a22 * bX - a12 * bY);
    out.y = det * (a11 * bY - a21 * bX);
    return out;
  },

  Abs: function () {
    this.col1.Abs();
    this.col2.Abs();
  },

  col1: new $.Engine.Math.Vector2D(),
  col2: new $.Engine.Math.Vector2D()
});